package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Apply;
import com.gxuwz.zlc.entity.Audition;
import com.gxuwz.zlc.service.ApplyService;
import com.gxuwz.zlc.service.AuditionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学生申请
 */
@Controller
@RequestMapping("/student")
public class ApplyController {

    @Autowired
    private ApplyService applyService;

    // 面试表
    @Autowired
    private AuditionService auditionService;

    // 查询所有
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Apply> findApply(){
        List<Apply> applyList = applyService.findApply();
        return applyList;
    }

    // 添加
    @RequestMapping("/add")
    @ResponseBody
    public Map addApply(@RequestBody Apply apply){
        Map<String,String> map = new HashMap<>();
        try {
            apply.setApplyTime(new Date());
            applyService.addApply(apply);
            map.put("code","200");
            map.put("msg","添加成功");
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","添加成功");
            e.printStackTrace();
        }
        return map;
    }

    // 修改
    @RequestMapping("/update")
    @ResponseBody
    public Map updateApply(@RequestBody Apply apply){
        System.out.println(apply);
        applyService.updateApply(apply);
        if ("1".equals(apply.getIsInterview())){
            Audition audition = new Audition();
            audition.setStuId(apply.getStuId());
            audition.setStuName(apply.getStuName());
            audition.setWorkName(apply.getWorkName());
            auditionService.addAudition(audition);
        }
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    // 删除
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteApply(@RequestParam(name = "applyId") Integer applyId){
        applyService.deleteApply(applyId);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("mag","删除成功");
        return map;
    }

    /**
     * 查询一个
     * @param applyId
     * @return
     */
    @GetMapping("/findOne/{applyId}")
    @ResponseBody
    public Result get(@PathVariable Integer applyId){
        Apply applyById = applyService.findApplyById(applyId);
        // code=0,msg=成功
        return Result.success(applyById);
    }

    /**
     * 根据学号查询报名信息
     * @param stuId
     * @return
     */
    @GetMapping("/selectOne/{stuId}")
    @ResponseBody
    public Result getByStuId(@PathVariable String stuId){
        Apply applyByStuId = applyService.findApplyByStuId(stuId);
        // code=0,msg=成功
        return Result.success(applyByStuId);
    }


}
